<!--消息通知-->
<template>
  <div class="promotion-container">
    <!-- 顶部导航栏 -->
    <el-tabs v-model="activeTab" class="nav-tabs">
      <el-tab-pane label="优惠活动" name="promotions"></el-tab-pane>
      <el-tab-pane label="我的资产" name="assets"></el-tab-pane>
      <el-tab-pane label="物流助手" name="logistics"></el-tab-pane>
    </el-tabs>

    <!-- 活动列表 -->
    <div v-if="activeTab === 'promotions'" class="promotions-list">
      <div
        v-for="(item, index) in promotions"
        :key="index"
        class="promotion-item"
      >
        <div class="item-header">
          <span class="title">{{ item.title }}</span>
          <span class="date">{{ item.date }}</span>
        </div>
        <div class="item-content">
          <img
            :src="item.imageUrl"
            alt="活动图片"
            class="image-placeholder"
            @error="handleImageError(index)"
          />
          <div class="details">
            <p>{{ item.description }}</p>
            <el-button link type="primary" @click="viewDetails(item.id)">
              查看详情 &gt;&gt;
            </el-button>
          </div>
        </div>
      </div>
      <div v-if="!promotions.length" class="no-data">暂无活动信息</div>
    </div>

    <!-- 分页控件 -->
    <el-pagination
      v-if="activeTab === 'promotions'"
      v-model:currentPage="pagination.currentPage"
      :page-sizes="[10, 20, 50]"
      :page-size="pagination.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import axios from 'axios';

const activeTab = ref('promotions');
const promotions = ref([]);
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
});

// 模拟接口地址
const apiUrl = '/api/promotions';

const getPromotions = async (params) => {
  try {
    const response = await axios.get(apiUrl, { params });
    return response.data;
  } catch (error) {
    console.error('获取活动列表失败:', error);
    return { data: [], total: 0 };
  }
};

const fetchPromotions = async () => {
  const { data, total } = await getPromotions({
    page: pagination.currentPage,
    limit: pagination.pageSize
  });
  promotions.value = data;
  pagination.total = total;
};

fetchPromotions();

const handleSizeChange = (pageSize) => {
  pagination.pageSize = pageSize;
  fetchPromotions();
};

const handleCurrentChange = (currentPage) => {
  pagination.currentPage = currentPage;
  fetchPromotions();
};

const handleImageError = (index) => {
  // 替换为默认图片
  promotions.value[index].imageUrl = '/default-image.png';
};

const viewDetails = (id) => {
  // 处理查看详情逻辑，例如导航到详情页
  console.log('查看活动详情:', id);
};
</script>

<style scoped>
.nav-tabs {
  margin-bottom: 20px;
}

.promotion-item {
  border-bottom: 1px dashed #ebeef5;
  padding: 20px 0;
}

.item-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}

.title {
  font-weight: bold;
}

.date {
  color: #999;
}

.image-placeholder {
  width: 300px;
  height: 200px;
  object-fit: cover;
  margin-right: 20px;
  background-color: #f5f5f5;
}

.details p {
  margin-bottom: 10px;
  max-height: 60px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
}

.no-data {
  text-align: center;
  color: #999;
  margin-top: 20px;
}
</style>